import Vue from 'vue'
import Vuex from 'vuex'
import $H from '../common/request'
Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		user: null,
		token: null,
		uploadList: [],
		downloadList: []
	},
	actions: {
		initList({
			state
		}) {
			if (state.user) {
				let d = uni.getStorageSync('dlist_' + state.user.id)
				let u = uni.getStorageSync('ulist_' + state.user.id)
				state.downloadList = d ? JSON.parse(d) : []
				state.uploadList = u ? JSON.parse(u) : []
			}
		},
		getShareUrl({
			state
		}) {
			// // #ifndef H5
			uni.getClipboardData({
				success: (res) => {
					if (res.data.includes('http://127.0.0.1:7001')) {
						const key = res.data.substring(
							res.data.lastIndexOf('\/') + 1, res.data
							.length)
						// 分享链接
						if (!key) {
							return
						}
						uni.showModal({
							content: '检测到分享内容，是否打开?',
							success: (res) => {
								if (res.confirm) {
									// 打开
									uni.navigateTo({
										url: '/pages/shareurl/shareurl?key=' +
											key
									})

									// 清空
									uni.setClipboardData({
										data: "",
									})
								}
							}
						})
					}
				}
			})
			// #endif
		},
		clearList({
			state
		}) {
			uni.removeStorageSync('dlist_' + state.user.id)
			uni.removeStorageSync('ulist_' + state.user.id)
			state.uploadList = []
			state.downloadList = []
		},
		createDownloadList({
			state
		}, obj) {
			state.downloadList.unshift(obj)
			uni.setStorage({
				key: "dlist_" + state.user.id, // 加用户ID区分多账号登录w
				data: JSON.stringify(state.downloadList)
			})
		},
		updateDownloadList({
			state
		}, obj) {
			let index = state.downloadList.findIndex(item => item.key === obj.key)
			if (index !== -1) {
				state.downloadList[index].progress = obj.progress
				state.downloadList[index].status = obj.status
				uni.setStorage({
					key: "dlist_" + state.user.id, // 加用户ID区分多账号登录w
					data: JSON.stringify(state.downloadList)
				})
			}
		},
		// 上传
		createUploadJob({
			state
		}, obj) {
			state.uploadList.unshift(obj)
			uni.setStorage({
				key: "ulist_" + state.user.id, // 加用户ID区分多账号登录w
				data: JSON.stringify(state.uploadList)
			})
		},
		// 更新上传任务
		updateUploadJob({
			state
		}, obj) {
			let index = state.uploadList.findIndex(item => item.key === obj.key)
			if (index !== -1) {
				state.uploadList[index].progress = obj.progress
				state.uploadList[index].status = obj.status
				uni.setStorage({
					key: "ulist_" + state.user.id, // 加用户ID区分多账号登录w
					data: JSON.stringify(state.uploadList)
				})
			}
		},
		logout({
			state
		}) {
			$H.get('/logout')
			state.user = null
			state.token = null
			uni.removeStorageSync('user')
			uni.removeStorageSync('token')
			uni.removeStorageSync('dirs')
			uni.reLaunch({
				url: '../login/login'
			})
		},
		login({
			state
		}, user) {
			state.user = user,
				state.token = user.token
			uni.setStorageSync('user', JSON.stringify(user))
			uni.setStorageSync('token', user.token)
		},
		initUser({
			state
		}) {
			let user = uni.getStorageSync('user')
			if (user) {
				state.user = JSON.parse(user)
				state.token = state.user.token
			}
		},
		updateSize({
			state
		}, size) {
			state.user.total_size = size.total_size
			state.user.used_size = size.used_size
		}
	}
})
